﻿using System;
using System.Data.SqlClient;
using AuctionSite.Entities;

namespace AuctionSite.DAL
{
    public class LanceDAL
    {
        public Lance Incluir(Lance lance)
        {
        
            SqlConnection cn = new SqlConnection();
            try
            {

                cn.ConnectionString = Dados.StringDeConexao;
                //command
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = cn;
            
                //cmd.CommandText = "UPDATE produtos SET comprador=@comprador, prod_valor=@prod_valor,prod_data_lance=@data WHERE prod_id=@prod_id";
                cmd.CommandText = "INSERT INTO lances (usu_id,prod_id,lance_valor,lance_hora) VALUES (@usu_id,@prod_id,@lance_valor,@lance_hora);select @@IDENTITY;";
                cmd.Parameters.AddWithValue("@usu_id", lance.UsuarioId);
                cmd.Parameters.AddWithValue("@prod_id", lance.ProdutoId);
                cmd.Parameters.AddWithValue("@lance_valor", lance.Valor);
                cmd.Parameters.AddWithValue("@lance_hora", lance.Data);
                
                
                cn.Open();
                lance.Id = Convert.ToInt32(cmd.ExecuteScalar());

                return lance;
            }
            catch (SqlException ex)
            {
                throw new Exception("Servidor SQL Erro:" + ex.Number);
            }
            catch (Exception ex)
            {
                throw new Exception("Erro:" + ex.Message);
            }
            finally
            {
                cn.Close();
            }
        }
    }
}